package com.fingard.dsp.bank.directbank.wzcb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;


/**
 * 温州银行单笔付款查询
 */
public class WZCB01SinglePay extends WZCB01Base implements ISinglePay {
    /**
     * 支付
     * @param p_req1908
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try{
            //拼接请求报文体
            WZCB01Header header = new WZCB01Header();
            header.txCode="E101";//SQEPay.101.01.01
            header.orderNo = p_req1908.singleDetail.askNumber;
            XmlTextWriter xmlReq = new XmlTextWriter();
            writeReqHeader(xmlReq,header);
            XElement body =new XElement("Body","");
            body.addChild(new XElement("BusCode","0"));//付款账号1：温州银行行内转账6：小额转账7：大额转账 0默认
            body.addChild(new XElement("BusAttr","02001"));//必填 02001 （保留为接口扩展用）
            body.addChild(new XElement("CurCode","1"));//必填 1 （保留为接口扩展用）
            body.addChild(new XElement("CurType","0"));//必填 0 （保留为接口扩展用）
            body.addChild(new XElement("TransAmt",p_req1908.singleDetail.payAmount));//交易金额
            body.addChild(new XElement("PayAcctNo",p_req1908.singleDetail.payAct));//       账号
            body.addChild(new XElement("PayAcctSeqno","0"));//必填 0 （保留为接口扩展用）
            body.addChild(new XElement("PaySysCode",""));//付款账号系统联行号
            body.addChild(new XElement("PayCityCode",""));//        付款账号所在城市
            body.addChild(new XElement("VouType",""));//凭证类型
            body.addChild(new XElement("VouNo",""));//        凭证号码
            body.addChild(new XElement("RcvAcctNo",p_req1908.singleDetail.recAct));//收款账号
            body.addChild(new XElement("RcvAcctSeqno","0"));//        收款账户序号
            body.addChild(new XElement("RcvBusCode",""));//收款业务种类（定期转账保留字段，目前无该业务）
            body.addChild(new XElement("RcvBusType",""));//收款业务类型（定期转账保留字段，目前无该业务）
            body.addChild(new XElement("RcvAcctName",p_req1908.singleDetail.recName));//收款户名
            body.addChild(new XElement("RcvSysCode",p_req1908.singleDetail.cnapsCode));//        收款账号系统联行号
            body.addChild(new XElement("RcvCityCode",p_req1908.singleDetail.recArea));//收款账号所在城市
            body.addChild(new XElement("OppVouType",""));//        凭证类型
            body.addChild(new XElement("OppVouNo",""));//凭证号码
            body.addChild(new XElement("CustAbs",p_req1908.singleDetail.purpose));//        摘要
            nodeAnalysis(p_req1908.singleDetail.srcXElement,body,getCharset());
            xmlReq.writeXElement(body);
            xmlReq.writeEndDocument();
            //组装head
            String head=getHead("E101","SQEPay.101.01.01");
            //发送请求报文
            String[] bankResult = sendToBank(xmlReq.toXmlString(),head);
            //处理响应报文
            if(bankResult[0].length()==0){
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                if(tmpBkReader!=null){
                    String respCode = tmpBkReader.getFirstText("/Response/Head/RefuseCode");
                    String respInfo = tmpBkReader.getFirstText("/Response/Head/RefuseReason");
                    detail.respCode = "0";
                    detail.respInfo = respInfo;
                    detail.reqSeqID = p_req1908.singleDetail.askNumber;
                    detail.transState = respCode;
                    detail.payInfoCode = detail.transState;
                    detail.transAmt = "";
                }else{
                    detail.respCode ="-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            }else if (bankResult[0].equals(ConnState.FAIL)) {
                detail.respCode = "0";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
                detail.transState = PaymentState.FAIL_Payment;
                detail.payInfoCode = bankResult[0];
            }else{
                detail.respCode ="-1";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
            }
        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    /**
     * 查询支付结果
     * @param p_req1944
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try{
            //拼接请求报文体
            WZCB01Header header = new WZCB01Header();
            header.txCode="E108";//SQEPay.108.01.01
            header.orderNo = p_req1944.askNumber;
            XmlTextWriter xmlReq = new XmlTextWriter();
            writeReqHeader(xmlReq,header);
            XElement body =new XElement("Body","");
            body.addChild(new XElement("BusCode", "2"));//1：通过交易流水查询 2：通过订单号查询
            body.addChild(new XElement("StartDate", Format.Date10Format.format(p_req1944.getBgnDateTime())));//交易日期
            body.addChild(new XElement("EndSeqno", ""));//流水号
            body.addChild(new XElement("OriginNo", p_req1944.askNumber));//原订单号
            xmlReq.writeXElement(body);
            xmlReq.writeEndDocument();
            //组装head
            String head=getHead("E108","SQEPay.108.01.01");
            //发送请求报文
            String[] bankResult = sendToBank(xmlReq.toXmlString(),head);
            if (bankResult[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String respCode = tmpBkReader.getFirstText("/Response/Head/RefuseCode");
                    String respInfo = tmpBkReader.getFirstText("/Response/Head/RefuseReason");
                    if ("0000".equals(respCode)) {
                        String state = tmpBkReader.getFirstText("/Response/Body/DealStatus");//交易状态
                        String errorMessage = tmpBkReader.getFirstText("/Response/Body/CustAbs");//失败原因
                        detail.respCode = "0";
                        detail.respInfo = StringHelper.hasAnyChar(errorMessage)?errorMessage:respInfo;
                        //０：正常(非最终状态) １：发送核心待校验(非最终状态)  6：核心记账不成功(非最终状态)
                        //８：成功完成(最终状态) ９：失败(最终状态)
                        detail.transState = state;
                        detail.reqSeqID = tmpBkReader.getFirstText("/Response/Body/OriginNo");
                        detail.payInfoCode = detail.transState;
                        detail.transAmt = tmpBkReader.getFirstText("/Response/Body/TransAmt");
                    }else {
                        detail.respCode = "-1";
                        detail.respInfo = respInfo;
                    }
                }else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            }else {
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
